package com.cats.template.sys.pojo.dto;

import com.cats.template.sys.pojo.model.BaseModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author duxiaobo
 * @date 2021/8/94:16 下午
 */
@Getter
public class BaseDto<T> implements Serializable {
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    private Integer orderNumber;
    @JsonIgnore
    private Long createdBy;
    @JsonIgnore
    private Long updatedBy;
    private LocalDateTime createdAt;
    @JsonIgnore
    private LocalDateTime updatedAt;
    @JsonIgnore
    private Boolean deleteFlg;


    public <Dto, Model extends BaseModel> Model parseModel(Class<Model> clazz) {
        if (Objects.isNull((T) this)) {
            return null;
        } else {
            Model model = BeanUtils.instantiateClass(clazz);
            BeanUtils.copyProperties((T) this, model);
            return model;
        }
    }

    public T setId(Long id) {
        this.id = id;
        return (T) this;
    }

    public T setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
        return (T) this;
    }

    public T setUpdatedBy(Long updatedBy) {
        this.updatedBy = updatedBy;
        return (T) this;
    }

    public T setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
        return (T) this;
    }

    public T setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
        return (T) this;
    }

    public T setDeleteFlg(Boolean deleteFlg) {
        this.deleteFlg = deleteFlg;
        return (T) this;
    }

    public T setOrderNumber(Integer orderNumber) {
        this.orderNumber = orderNumber;
        return (T) this;
    }
}
